﻿/***********************************************************************
 * <copyright file="RoleRepository.cs" company="B/S JSC">
 * -->    Copyright (C) statement. All right reserved
 * </copyright>
 * 
 * Created:   Hoang Bich Son 
 * Email:    sonhb@newman.vn
 * Website:
 * Create Date: Friday, March 21, 2014
 * Usage: 
 * 
 * RevisionHistory: 
 * Date         Author               Description 
 * 
 * ************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using DxMvcApplication.Repositories.Core;
using DxMvcApplication.Repositories.Core.Security;
using DxMvcApplication.SecurityServiceReference;
using NmResources;

namespace DxMvcApplication.Repositories.Implementations.Security
{
    /// <summary>
    /// RoleRepository class
    /// </summary>
    public class RoleRepository : SecurityRepositoryBase, IRoleRepository
    {
        public int GetPageSize()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Gets the list.
        /// </summary>
        /// <param name="criterion">The criterion.</param>
        /// <returns></returns>
        public List<RoleDto> GetList(Criterion criterion = null)
        {
            var request = new RoleRequest().Prepare();
            request.LoadOptions = new[] { "Roles", "IsActive" };
            request.Criteria = new RoleCriteria { IsActive = true };
            var response = SecurityServiceClient.GetRoles(request);
            Correlate(request, response);

            return response.Roles == null ? null : response.Roles.ToList();
        }

        /// <summary>
        /// Gets the paged.
        /// </summary>
        /// <param name="totalPage">The total page.</param>
        /// <param name="currentPage">The current page.</param>
        /// <returns></returns>
        public List<RoleDto> GetPaged(ref int totalPage, int currentPage = 1)
        {
            var request = new RoleRequest().Prepare();
            request.LoadOptions = new[] { "Roles" };
            request.OptionTypes = OptionType.IsPaging;
            request.CurrentPage = currentPage;
            request.TotalPage = totalPage;
            var response = SecurityServiceClient.GetRoles(request);
            Correlate(request, response);
            totalPage = response.TotalPage;
            return response.Roles == null ? null : response.Roles.ToList();
        }

        /// <summary>
        /// Gets the specified identifier.
        /// </summary>
        /// <param name="id">The identifier.</param>
        /// <returns></returns>
        public RoleDto Get(int id)
        {
            var request = new RoleRequest().Prepare();
            request.LoadOptions = new[] { "Role" };
            request.Criteria = new RoleCriteria { RoleId = id };

            var response = SecurityServiceClient.GetRoles(request);
            Correlate(request, response);
            return response.Role;
        }

        /// <summary>
        /// Inserts the specified role.
        /// </summary>
        /// <param name="role">The role.</param>
        /// <returns></returns>
        public string Insert(RoleDto role)
        {
            var request = new RoleRequest().Prepare();
            request.Action = "Insert";
            request.Role = role;

            var response = SecurityServiceClient.SetRoles(request);
            Correlate(request, response);
            // These messages are for public consumption. Includes validation errors.
            if (response.Acknowledge == AcknowledgeType.Failure)
                throw new ApplicationException(response.Message);
            return response.MessageCode.MapperResource();
        }

        /// <summary>
        /// Updates the specified role.
        /// </summary>
        /// <param name="role">The role.</param>
        /// <returns></returns>
        public string Update(RoleDto role)
        {
            var request = new RoleRequest().Prepare();
            request.Action = "Update";
            request.Role = role;
            var response = SecurityServiceClient.SetRoles(request);
            Correlate(request, response);
            // These messages are for public consumption. Includes validation errors.
            if (response.Acknowledge == AcknowledgeType.Failure)
                throw new ApplicationException(response.Message);
            return response.MessageCode.MapperResource();
        }

        /// <summary>
        /// Deletes the specified identifier list.
        /// </summary>
        /// <param name="idList">The identifier list.</param>
        /// <returns></returns>
        public string Delete(IList<int> idList)
        {
            var request = new RoleRequest().Prepare();
            request.Action = "Delete";
            request.Criteria = new RoleCriteria { IdList = string.Join(",", idList) };
            var response = SecurityServiceClient.SetRoles(request);
            Correlate(request, response);
            // These messages are for public consumption. Includes validation errors.
            if (response.Acknowledge == AcknowledgeType.Failure)
                throw new ApplicationException(response.Message);
            return response.MessageCode.MapperResource();
        }

        /// <summary>
        /// Gets the by user identifier.
        /// </summary>
        /// <param name="id">The identifier.</param>
        /// <returns></returns>
        public string[] GetByUserId(int? id)
        {
            if (id == null) return null;
            var request = new RoleRequest().Prepare();
            request.LoadOptions = new[] { "RoleByUser" };
            request.Criteria = new RoleCriteria { UserId = (int)id };
            var response = SecurityServiceClient.GetRoles(request);
            Correlate(request, response);
            return response.RolesByUsername;
        }

        /// <summary>
        /// Gets the role in site.
        /// </summary>
        /// <param name="roleId">The role identifier.</param>
        /// <returns></returns>
        public List<RoleInSiteDto> GetRoleInSite(int roleId)
        {
            var request = new RoleRequest().Prepare();
            request.LoadOptions = new[] { "RoleInSiteByRole" };
            request.Criteria = new RoleCriteria { RoleId = roleId };
            var response = SecurityServiceClient.GetRoles(request);
            Correlate(request, response);
            return response.RoleInSite.ToList();
        }

        /// <summary>
        /// Gets the role in site by site.
        /// </summary>
        /// <param name="siteId">The site identifier.</param>
        /// <returns></returns>
        public List<RoleInSiteDto> GetRoleInSiteBySite(int siteId)
        {
            var request = new RoleRequest().Prepare();
            request.LoadOptions = new[] { "RoleInSiteBySite" };
            request.Criteria = new RoleCriteria { SiteId = siteId };
            var response = SecurityServiceClient.GetRoles(request);
            Correlate(request, response);
            return response.RoleInSite.ToList();
        }

        /// <summary>
        /// Grants the role permission.
        /// </summary>
        /// <param name="roleId">The role identifier.</param>
        /// <param name="objs">The objs.</param>
        /// <returns></returns>
        public string AssignToRole(int roleId, List<RoleInSiteDto> objs)
        {
            var request = new RoleInSiteRequest().Prepare();
            request.Action = "Insert";
            request.RoleInSites = objs.ToArray();
            request.Criteria = new RoleInSiteCriteria { RoleId = roleId };
            var response = SecurityServiceClient.SetRoleInSites(request);
            Correlate(request, response);
            // These messages are for public consumption. Includes validation errors.
            if (response.Acknowledge == AcknowledgeType.Failure)
                throw new ApplicationException(response.Message);
            return response.MessageCode.MapperResource();
        }
    }
}